// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129

// 引入uni-id-common模块
const {
	userInfo
} = require('os');
const uniIdCommon = require('uni-id-common')
const db = uniCloud.database();
const userscores = db.collection("uni-id-scores") //积分
const usersInfo = db.collection('uni-id-users') //用户信息
module.exports = {
	_before: async function() { // 通用预处理器

		let body = JSON.parse(this.getHttpInfo().body)

		this.clientInfo = body.clientInfo;

		this.uniIdCommon = uniIdCommon.createInstance({
			clientInfo: this.clientInfo
		})
		let r = await this.uniIdCommon.checkToken(this.clientInfo.uniIdToken)
		if (r.errCode) {
			// 如果token校验出错，则抛出错误
			throw r
		} else {
			// 通过token校验则，拿去用户id
			this.current_uid = r.uid
		}
	},
	/**
	 * 获取用户积分行为列表
	 * @param {string} param1 参数1描述
	 * @returns {object} 返回值描述
	 */

	async getScore() {

		let res = await userscores.where({
			user_id: this.current_uid
		}).get()
		return res;

	},
	/**
	 * 获取用户信息
	 */
	async getUserInfo() {

		// console.log(this.current_uid, "=====")
		const res = await usersInfo.where({
			_id: this.current_uid
		}).limit(1).get()
		const data=res.data[0]
		console.log(data.username,"+++++")
		return {
			error:0,
			data:{
				_id:data._id,
				username:data.username,
				score:data.score,
				avatar:data.avatar,
				gender:data.gender
			}
		};
	}

}